#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Figure 3
#####

library(here)
library(data.table)
library(lfe)
library(ggplot2)
library(ggstance)

# load the behavioral data if not already in workspace
if(!exists("behav")){
  behav <- fread(here("data", "behav.csv"))
}


# Fit model ---------------------------------------------------------------

did <- felm(tokens ~ female_partner + foreign_partner + CH:foreign_partner + CS:foreign_partner + NM:foreign_partner | game_NR + ResponseId | 0 | ResponseId, data = behav)

# Plot prep ---------------------------------------------------------------

plot_matB <- data.frame(est = rep(0, 3), lower95 = 0, upper95 = 0, lower90 = 0, upper90 = 0, x = 0, D = NA, stringsAsFactors = F)
plot_matB[ 1:3, ] <- data.frame(extractPlotInfo(did, 3:5), 1, c("Common Humanity", "Countering Stereotypes", "Norms"), stringsAsFactors = F)

plot_matB$x <- 1:3

# Plot & Save -------------------------------------------------------------
fig_3 <- ggplot(data = plot_matB, aes(x = est, y = x, color = D, shape = D)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggstance::geom_linerangeh(aes(xmin = lower95, xmax = upper95), position = ggstance::position_dodgev(.5)) +
  ggstance::geom_linerangeh(aes(xmin = lower90, xmax = upper90), position = ggstance::position_dodgev(.5), size = 1.2) +
  geom_point(position = ggstance::position_dodgev(.5), size = 3, fill = "white") +
  xlab("Effect on difference in tokens given to foreign and native partners") +
  ylab(NULL) +
  scale_y_continuous(
    breaks = 1:3,
    labels = c("Common\nHumanity", "Countering\nStereotypes", "Norms"),
    limits = c(0.5, 3.5),
    expand = expansion(0,0)
  ) +
  scale_color_grey(name = "Treatment:", guide = guide_legend(reverse=TRUE), end = .7) +
  scale_shape_manual(values = c(15, 21,17), name = "Treatment:", guide = guide_legend(reverse=TRUE)) +
  theme_minimal() +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = "none",
    axis.title = element_text(size = 7),
    axis.text = element_text(size = 7),
  )

psrm_width = 4.88284
ggsave(here("main_text", "figure_3.pdf"), fig_3, height = psrm_width * .35, width = psrm_width)
